#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023-04-20 21:57
# @Author  : wsh
# @File    : depart_mapper.py
from typing import Union, List

from model.db import reservoir_sensitivity, Tb_Depart, TbCompanies
from model.db.mapper import get_company
from model.params.depart_params import DepartInfoParam, DepartGetIdParam, DepartListItemPara
from util import DataNotExist


def add_depart(depart: Tb_Depart) -> Tb_Depart:
    reservoir_sensitivity.add(depart)
    reservoir_sensitivity.commit()
    reservoir_sensitivity.flush(depart)
    # company = reservoir_sensitivity.query(Tb_Depart).filter_by(company_name=company_name).first()
    # if company is None:
    #     raise DataNotExist("公司")
    return


def get_depart_by_depart_id(belong_post: str) -> Tb_Depart:
    depart = reservoir_sensitivity.query(Tb_Depart).filter_by(depart_name=belong_post).first()
    if depart is None:
        raise DataNotExist("职位")
    return depart


def get_dapart_list(company_id: int) -> Union[List[Tb_Depart], None]:
    departs = reservoir_sensitivity.query(Tb_Depart.depart_id, Tb_Depart.depart_name).filter_by(
        company_id=company_id).all()

    return departs


def get_dapart_detail_list():
    departs = reservoir_sensitivity.query(Tb_Depart.depart_id, Tb_Depart.Remark, Tb_Depart.depart_name,
                                          TbCompanies.company_name).join(Tb_Depart,
                                                                         Tb_Depart.company_id == TbCompanies.company_id).all()
    return departs


def update_depart_info(add, delete, update):
    """
    更新开发单元
    """
    try:
        if len(add) != 0:
            for item in add:
                company = get_company(item['company_name'])
                company = Tb_Depart(
                    company_id=company.company_id,
                    Remark=item['Remark'],
                    depart_name=item['depart_name']
                )
                reservoir_sensitivity.add(company)
                reservoir_sensitivity.commit()
                item['depart_id'] = company.depart_id
        if len(delete) != 0:
            for item in delete:
                reservoir_sensitivity.query(Tb_Depart).filter(Tb_Depart.depart_id == item['depart_id']) \
                    .delete(synchronize_session=False)

        if len(update) != 0:
            for item in update:
                company = get_company(item['company_name'])
                reservoir_sensitivity.query(Tb_Depart).filter(Tb_Depart.depart_id == item['depart_id']) \
                    .update({
                    "company_id": company.company_id,
                    "Remark": item['Remark'],
                    "depart_name": item['depart_name']
                })
        reservoir_sensitivity.commit()
        reservoir_sensitivity.flush()
        return add
    except Exception as e:
        reservoir_sensitivity.rollback()
        raise e
